package com.xw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xw.common.TokenHandler;
import com.xw.constant.RedisKeyConstant;
import com.xw.dao.CommentDao;
import com.xw.dao.ThumbsDao;
import com.xw.entity.Thumbs;
import com.xw.entity.User;
import com.xw.rep.QueryNumberRep;
import com.xw.req.PageRequest;
import com.xw.service.ThumbsService;
import com.xw.util.RedisCacheUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 针对表【thumbs】的数据库操作Service实现
 * @Author: 72478
 * @Date: 2024/12/18 16:53
 * @Version: 1.0
 */
@Service("thumbsService")
public class ThumbsServiceImpl implements ThumbsService {
    @Resource
    private ThumbsDao thumbsDao;
    @Resource
    private CommentDao commentDao;
    @Resource
    private RedisCacheUtil redisCacheUtil;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public PageInfo<Thumbs> queryByPage(PageRequest<Thumbs> pageRequest) {
        User userInfo = TokenHandler.getUserInfo();
        pageRequest.getQ().setUserId(userInfo.getId());
        //开始分页
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Thumbs> list = this.thumbsDao.queryAll(pageRequest.getQ());
        PageInfo<Thumbs> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public List<Map<String, Object>> getHighRankPosts() {
        // 定义 Redis 缓存的键
        String cacheKey = RedisKeyConstant.REDIS_KEY_HIGH_RANK_POSTS;

        // 尝试从 Redis 中获取缓存
        List highRankPosts = redisCacheUtil.getCache(cacheKey, List.class);
        if (highRankPosts != null && !highRankPosts.isEmpty()) {
            // 如果缓存中有数据，则直接返回
            return highRankPosts;
        }

        // 如果缓存中没有数据，则从数据库查询
        highRankPosts = thumbsDao.getHighRankPosts();

        // 将查询到的数据存入 Redis 缓存
        if (highRankPosts != null && !highRankPosts.isEmpty()) {
            redisCacheUtil.setCache(cacheKey, highRankPosts);
        }

        return highRankPosts;
    }


    @Override
    public List<QueryNumberRep> queryNumber(Integer linkId) {
        User userInfo = TokenHandler.getUserInfo();
        List<QueryNumberRep> queryNumberReps = new ArrayList<>();
        queryNumberReps.add(new QueryNumberRep(false,0,0));
        queryNumberReps.add(new QueryNumberRep(false,2,0));
        Thumbs thumbsQuery = new Thumbs();
        thumbsQuery.setLinkId(linkId);
        List<Thumbs> thumbs = this.thumbsDao.queryAll(thumbsQuery);
        List<Thumbs> likeNumList = thumbs.stream().filter(u -> u.getType().compareTo(0) == 0).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(likeNumList)) {
            Optional<Thumbs> any = likeNumList.stream().filter(u -> u.getUserId().compareTo(userInfo.getId()) == 0).findAny();
            for (QueryNumberRep queryNumberRep : queryNumberReps) {
                if (queryNumberRep.getState().compareTo(0) == 0) {
                    queryNumberRep.setNumber(likeNumList.size());
                    queryNumberRep.setOpHso(any.isPresent());
                }
            }
        }
        List<Thumbs> collectionNumList = thumbs.stream().filter(u -> u.getType().compareTo(2) == 0).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collectionNumList)) {
            Optional<Thumbs> any = collectionNumList.stream().filter(u -> u.getUserId().compareTo(userInfo.getId()) == 0).findAny();
            for (QueryNumberRep queryNumberRep : queryNumberReps) {
                if (queryNumberRep.getState().compareTo(2) == 0) {
                    queryNumberRep.setNumber(collectionNumList.size());
                    queryNumberRep.setOpHso(any.isPresent());
                }
            }
        }
        return queryNumberReps;
    }
}
